<?php
namespace Modules\ShopApi\Models; 
use Modules\Admin\Services\UserService;
use Illuminate\Support\Facades\DB;
class ShopCart extends BaseApiModel
{	
	/**
     * @name  加入购物车 
     */
	public function add_cart($data){
		$user_info = (new UserService())->my();
		$id = $this->where(['product_id'=>$data['product_id'],'specs_id'=>$data['specs_id'],'user_id'=>$user_info['id']])->value('id');
		if($id){
			$res = $this->where(['id'=>$id])->increment('num',$data['num']);
		}else{
			$money = DB::table('shop_product_specs')->where(['id'=>$data['specs_id']])->value('money');
			$res = $this->insert([
				'user_id'=>$user_info['id'],
				'product_id'=>$data['product_id'],
				'num'=>$data['num'],
				'specs_id'=>$data['specs_id'],
				'money'=>$money,
				'specs'=>$data['specs'],
				'created_at'=>date('Y-m-d H:i:s'),
			]);
		}
		if($res){
			return $this->api_success(['data'=>[ 
				'cart_num'=>$this->where(['user_id'=>$user_info['id']])->sum('num')
			]]);
		}else{
			return $this->api_error();
		}
	}
	/**
     * @name 关联商品
     */
    public function product_to()
    {
        return $this->belongsTo('Modules\ShopApi\Models\ShopProduct','product_id','id');
    }
	/**
     * @name 关联规格
     */
    public function specs_to()
    {
        return $this->belongsTo('Modules\ShopApi\Models\ShopProductSpec','specs_id','id');
    }
	/**
     * @name  购物车列表 
     */
	public function cart_list(){
		$user_info = (new UserService())->my();
		if($user_info===false){
			return $this->api_success(['data'=>['status'=>0]]);
		}else if(isset($user_info['code']) && $user_info['code'] == 1003){
			return $this->api_error($user_info);
		}
		$info = $this->select('id','product_id','user_id','num','specs_id','specs','created_at')
				->whereHas('product_to',function($query){
					$query->where('status',1);
				})
				->with([
					'product_to'=>function($query){
						$query->select('id','name','image_id');
					},
					'product_to.image_one'=>function($query){
						$query->select('id','url','open');
					},
					'specs_to'=>function($query){
						$query->select('id','money','postage');
					}
				])
				->where(['user_id'=>$user_info['id']])
				->get()
				->toArray();
		return $this->api_success(['data'=>[
								'status'=>1,
								'info'=>$this->cart_list_data($info)
					]]);
	}
	/**
     * @name  通过id获取购物车数据 
     */
	public function ids_cart_list($ids){
		$info = $this->select('id','product_id','user_id','num','specs_id','specs','created_at')
				->with([
					'product_to'=>function($query){
						$query->select('id','name');
					},
					'specs_to'=>function($query){
						$query->select('id','money','postage');
					}
				])
				->whereIn('id',$ids)
				->get()
				->toArray();
		return $info;		
	}
	/**
     * @name  购物车列表数据处理
     */
	private function cart_list_data($info){
		$http = getHttpType();	
		foreach($info as $k=>$v){
			$info[$k]['name'] 		= $v['product_to']['name'];
			$info[$k]['money'] 		= $v['specs_to']['money'];
			$info[$k]['postage'] 	= $v['specs_to']['postage'];
			$info[$k]['total_money'] = sprintf("%.2f",($v['specs_to']['money'] + $v['specs_to']['postage']));
			$info[$k]['on'] = false;
			if($v['product_to']['image_one']['open'] == 1){
				$info[$k]['image_url'] = $http . $v['product_to']['image_one']['url'];
			}else{
				$info[$k]['image_url'] = $v['product_to']['image_one']['url'];
			}
			unset($info[$k]['user_id'],$info[$k]['specs_id'],$info[$k]['product_to'],$info[$k]['specs_to']);
		}
		return $info;
	}
	/**
     * @name  购物车加 
     */
	public function cart_add($id){
		if($this->where(['id'=>$id])->value('num')>=99){
			return $this->api_success();
		}
		if($this->where(['id'=>$id])->increment('num',1)){
			return $this->api_success();
		}else{
			return $this->api_error();
		};
	}
	/**
     * @name  购物车减 
     */
	public function cart_subtract($id){
		if($this->where(['id'=>$id])->value('num')<=1){
			return $this->api_success();
		}
		if($this->where(['id'=>$id])->decrement('num',1)){
			return $this->api_success();
		}else{
			return $this->api_error();
		};
	}
	/**
     * @name  购物车删除 
     */
	public function cart_delete($id){
		if($this->destroy($id)){
			return $this->api_success();
		}else{
			return $this->api_error();
		};
	}
}
